﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using System.Collections.ObjectModel;
using System.Net;

namespace SlaktDataReader
{
    class MarriageBookTextFile:TextFileBase
    {
        //private Dictionary<string, int> _fields = new Dictionary<string, int>();

        //public string FileName { get; set; }
        //public char Separator { get; set; }
        //public Encoding FileEncoding { get; set; }
        public List<MarriageBookRecord> Records { get; set; }

        private MarriageBook bk;

        public MarriageBookTextFile()
        {
            _fields.Add("Lopnr", -1);
            _fields.Add("Sidnr", -1);
            _fields.Add("Ordnr", -1);
            _fields.Add("Lysn1", -1);
            _fields.Add("Vdatum", -1);
            _fields.Add("Mtitel", -1);
            _fields.Add("Mfnamn", -1);
            _fields.Add("Menamn", -1);
            _fields.Add("Madress", -1);
            _fields.Add("Nmadress", -1);
            _fields.Add("Mforaldr", -1);
            _fields.Add("Malder", -1);

            _fields.Add("Ktitel", -1);
            _fields.Add("Kfnamn", -1);
            _fields.Add("Kenamn", -1);
            _fields.Add("Kadress", -1);
            _fields.Add("Nkadress", -1);
            _fields.Add("Kforaldr", -1);

            _fields.Add("Ovr1", -1);
            _fields.Add("Ovr2", -1);
            _fields.Add("Ovr3", -1);
            _fields.Add("Uppdaterat", -1);

            Separator = '\t';
            Records = new List<MarriageBookRecord>();
            FileEncoding = Encoding.UTF7;
        }

        public int Read(string FileName)
        {
            this.FileName = FileName;
            return Read();
        }

        //public int Read()
        //{
        //    int i, _LinesRead = 0;
        //    bool HeaderFound = false;
        //    string[] lines;
        //    try
        //    {
        //        if (FileName.ToUpper().StartsWith("HTTP://") || FileName.ToUpper().StartsWith("HTTPS://") || FileName.ToUpper().StartsWith("FTP://"))
        //        {
        //            if (Globals.BookDownloadPath == null)
        //                return 0;

        //            WebClient wc = new WebClient();


        //            if (!Directory.Exists(Globals.BookDownloadPath))
        //            {
        //                Directory.CreateDirectory(Globals.BookDownloadPath);
        //            }

        //            string tmpFileName = Globals.BookDownloadPath + new Uri(FileName).AbsolutePath;
        //            if (tmpFileName.Contains("getText.php/"))
        //            {
        //                tmpFileName = tmpFileName.Remove(tmpFileName.IndexOf("getText.php/"), 12);
        //            }
        //            wc.DownloadFile(FileName, tmpFileName);
        //            lines = File.ReadAllLines(tmpFileName, FileEncoding);
        //        }
        //        else
        //        {
        //            lines = File.ReadAllLines(FileName, FileEncoding);
        //        }
        //        if (lines.Length == 0)
        //        {
        //            return 0;
        //        }

        //        for (i = 0; i < lines.Length; i++)
        //        {
        //            if (!HeaderFound)
        //            {
        //                if (lines[i].ToUpper().Contains("LOPNR"))
        //                {
        //                    HeaderFound = _ParseHeader(lines[i]);
        //                    continue;
        //                }
        //                else
        //                {
        //                    _ParseHeaderInfoLine(lines[i]);
        //                    continue;
        //                }
        //            }
        //            else
        //            {
        //                _LinesRead += _ParseLine(lines[i]);
        //            }
        //        }
        //    }
        //    catch
        //    {
        //        return -1;
        //    }
        //    return _LinesRead;
        //}

        //private int _ParseLine(string line)
        //{
        //    string[] fields;
        //    if (line.Length > 0)
        //    {
        //        fields = line.Split(Separator);
        //        if (fields.Length >= _fields.Where(f => f.Value != -1).Count() - 1)
        //        {
        //            _AddRecord(fields);
        //            return 1;
        //        }
        //    }
        //    return 0;
        //}

        protected override void _AddRecord(string[] RowFields)
        {
            if (RowFields.Length > 0)
            {
                MarriageBookRecord r = new MarriageBookRecord();
                for (int i = 0; i < RowFields.Length; i++)
                {
                    if (_fields.ContainsValue(i))
                    {
                        try
                        {
                            switch (_fields.Where(f => f.Value == i).First().Key)
                            {
                                case "Lopnr":
                                    try
                                    {
                                        r.RowID = Convert.ToInt32(RowFields[i]);
                                    }
                                    catch
                                    {
                                        r.RowID = 0;
                                    }
                                    break;
                                case "Sidnr":
                                    r.PageNo = RowFields[i];
                                    break;
                                case "Ordnr":
                                    r.OrdNo = RowFields[i];
                                    break;
                                case "Lysn1":
                                    r.Banns = RowFields[i];
                                    break;
                                case "Vdatum":
                                    r.MarriageDate = RowFields[i];
                                    break;
                                case "Fnamn":
                                    r.FirstName = RowFields[i];
                                    break;
                                case "Mtitel":
                                    r.MaleTitle = RowFields[i];
                                    break;
                                case "Mfnamn":
                                    r.MaleFirstName = RowFields[i];
                                    break;
                                case "Menamn":
                                    r.MaleSurName = RowFields[i];
                                    break;
                                case "Madress":
                                    r.MaleAddress = RowFields[i];
                                    break;
                                case "Nmadress":
                                    r.NewMaleAddress = RowFields[i];
                                    break;
                                case "Mforaldr":
                                    r.MaleParents = RowFields[i];
                                    break;
                                case "Malder":
                                    r.MaleAge = RowFields[i];
                                    break;
                                case "Ktitel":
                                    r.FemaleTitle = RowFields[i];
                                    break;
                                case "Kfnamn":
                                    r.FemaleFirstName = RowFields[i];
                                    break;
                                case "Kenamn":
                                    r.FemaleSurName = RowFields[i];
                                    break;
                                case "Kadress":
                                    r.FemaleAddress = RowFields[i];
                                    break;
                                case "Nkadress":
                                    r.FemaleAge = RowFields[i];
                                    break;
                                case "Kforaldr":
                                    r.FemaleParents = RowFields[i];
                                    break;
                                case "Kalder":
                                    r.FemaleAge = RowFields[i];
                                    break;

                                case "Ovr1":
                                    r.Extra1 = RowFields[i];
                                    break;
                                case "Ovr2":
                                    r.Extra2 = RowFields[i];
                                    break;
                                case "Ovr3":
                                    r.Extra3 = RowFields[i];
                                    break;
                                case "Uppdaterat":
                                    r.Updated = RowFields[i];
                                    break;
                                default:
                                    break;
                            }
                        }
                        catch (Exception ex) { /*System.Windows.MessageBox.Show(ex.Message); */ }
                    }
                }
                if (r.IsChanged)
                {
                    r.IsChanged = false;
                    Records.Add(r);
                }
            }
        }

        //private void _ParseHeaderInfoLine(string line)
        //{

        //}

        //private bool _ParseHeader(string line)
        //{
        //    try
        //    {
        //        if (line.Length > 0)
        //        {
        //            string[] row = line.Split(Separator);
        //            for (int i = 0; i < row.Length; i++)
        //            {
        //                if (_fields.ContainsKey(row[i]))
        //                {
        //                    _fields[row[i]] = i;
        //                }
        //            }

        //            return true;
        //        }
        //    }
        //    catch
        //    {
        //    }

        //    return false;
        //}

        public bool Write(string FileName, MarriageBook book)
        {
            this.bk = book;
            this.FileName = FileName;
            return Write();
        }

        public bool Write()
        {
            if (FileName.ToUpper().EndsWith(".XML"))
            {
                return WriteXML();
            }
            else if (FileName.ToUpper().EndsWith(".TXT"))
            {
                return WriteTXT();
            }
            return false;
        }

        private bool WriteTXT()
        {
            return false;
        }

        private bool WriteXML()
        {
            try
            {
                //string LocationsFile = System.IO.Directory.GetCurrentDirectory() + "\\locations.dat";
                using (FileStream stream = new FileStream(FileName, FileMode.Create))
                {
                    XmlSerializer xs = new XmlSerializer(typeof(ObservableCollection<MarriageBook>));
                    using (var writer = new StreamWriter(stream))
                    {
                        xs.Serialize(writer, bk);
                    }
                }

            }
            catch { }
            return false;
        }



    }
}
